package or.exa.cangguan.config;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Result;
import or.exa.cangguan.mapper.PermissionMapper;
import or.exa.cangguan.mapper.RolePermissionMapper;
import or.exa.cangguan.mapper.UserInfoMapper;
import or.exa.cangguan.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;


@Slf4j
@Component
@WebFilter(urlPatterns = "/*")
public class Filter implements javax.servlet.Filter {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;



    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//
//        String url = httpServletRequest.getRequestURI().toString();
//        log.info("请求路径: {}", url);
//
//        if(httpServletRequest.getMethod().equals("OPTIONS")){
//            chain.doFilter(request, response);
//        }
//
//




//         登录验证
//        if (url.contains("login")) {
//            chain.doFilter(request, response);
//            return;
//        }
//        if (url.contains("permission")) {
//            chain.doFilter(request, response);
//            return;
//        }
//
//        Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            String headerName = headerNames.nextElement();
//            String value = httpServletRequest.getHeader(headerName);
//            log.info("Header: {} => {}", headerName, value);
//        }
//
//
//        String token = httpServletRequest.getHeader("token");
//        log.info("token: {}", token);
//
//
//
//        if (!StringUtils.hasLength(token)){
//            Result err = Result.err(500, "NOT_LOGIN");
//            String jsonString = JSONObject.toJSONString(err);
//            response.getWriter().write(jsonString);
//            return;
//        }
//
//        try {
//
//        DecodedJWT verify = JWTUtil.verify(token);
//        }catch (Exception e){
//            Result err = Result.err(500, "无效登录");
//            String jsonString = JSONObject.toJSONString(err);
//            response.getWriter().write(jsonString);
//            return;
//        }


        //验证权限
//        String username = verify.getClaim("username").asString();
//        log.info("用户名: {}", username);
//        Integer roleId = userInfoMapper.selectRoleIdByUsername(username);
//        List<Integer> integers = rolePermissionMapper.selectByRoleId(roleId);
//        Integer permissionId = permissionMapper.selectId(url);
//        int flag=0;
//        for (Integer integer : integers) {
//            if (integer.equals(permissionId)) {
//                flag++;
//            }
//        }
//        if(flag==0){
//            return ;
//        }


        chain.doFilter(request, response);

    }
}
